require 'gamifier'
require 'json'

Gamifier.set :uri => "http://sandbox.v2.badgeville.com/api/berlin/", :key => ENV.fetch('BADGEVILLE_KEY'), :enterprise_key => ENV.fetch('BADGEVILLE_ENTERPRISE_KEY')
Gamifier.logger.level = Logger::DEBUG

network = Gamifier.dsl do
  set :label, 'MyNetwork'
  set :email, 'dev@mynetwork.com'

  api_key do
    set :access, 'public'
  end

  api_key do
    set :access, 'private'
  end

  site 'dev.domain-test.crohr.me' do |site|
    set :url, 'dev.domain-test.crohr.me'

    unit :karma do
      set :label, 'Karma'
      set :abbreviation, 'kar'
    end

    behavior 'visit' do
      set :description, "visit"
      set :selector, {
        :verb => 'visit'
      }

      set :enable_count_limiting, false
      set :enable_rate_limiting, false

      set :hide_in_widgets, true
      set :icon, "http://s3.amazonaws.com/icons/icon1.png"
    end

    behavior 'signin' do
      set :description, "signin"
      set :selector, {
        :verb => 'signin'
      }

      set :enable_count_limiting, false
      set :enable_rate_limiting, true
      set :bucket_max_capacity, 2
      set :bucket_drain_rate, bucket_max_capacity/1.5

      set :hide_in_widgets, false
      set :icon, "http://s3.amazonaws.com/icons/icon1.png"
    end

    behavior 'comment' do
      set :description, "new comment"
      set :selector, {
        :verb => 'comment'
      }
      set :adjustment, {
        :points => 1, :karma => 1
      }

      set :enable_count_limiting, false
      # set :limit_per_player, 20

      set :enable_rate_limiting, true
      set :bucket_max_capacity, 2
      set :bucket_drain_rate, bucket_max_capacity/1.5

      set :hide_in_widgets, false
    end

    # behavior 'comment_databases' do
    #   set :description, "new comment in category databases"
    #   set :selector, {
    #     :verb => 'comment',
    #     :category_slug => 'databases'
    #   }
    #   set :adjustment, {
    #     :points => 1, :karma => 1
    #   }
    #
    #   set :enable_count_limiting, true
    #   set :limit_per_player, 10
    #   set :limit_field_scope, 'category_id'
    #
    #   set :enable_rate_limiting, true
    #   set :bucket_max_capacity, 2
    #   set :bucket_drain_rate, bucket_max_capacity/1.5
    #
    #   set :hide_in_widgets, false
    #   set :icon, "http://s3.amazonaws.com/icons/icon1.png"
    # end

    r_member = reward 'member' do
      set :reward_template, {
        :message => "Congrats, you signed in for the first time!"
      }
      set :components, [
        {:command => "count", :comparator => 1, :where => {:verb => 'signin', :player_id => "%player_id"}}
      ]
      set :adjustment, {
        :points => 2
      }
      set :active, true
      set :allow_duplicates, false
    end

    r_active_member = reward 'active member' do
      set :reward_template, {
        :message => "Looks like you like the site, thank you and keep up on being active on the site!"
      }
      set :components, [
        {:command => "count", :comparator => {"$gt" => 3}, :where => {:verb => 'signin', :player_id => "%player_id"}}
      ]
      set :adjustment, {
        :points => 5
      }
      set :active, true
      set :allow_duplicates, false
    end

    r_commenter = reward 'commenter' do
      set :reward_template, {
        :message => "Congrats, you commented on something!"
      }
      set :components, [
        {:command => "count", :comparator => 1, :where => {:verb => 'comment', :player_id => "%player_id"}}
      ]
      set :adjustment, {
        :points => 1
      }
      set :active, true
      set :allow_duplicates, false
    end

    r_active_commenter = reward 'active commenter' do
      set :reward_template, {
        :message => "You have commented more than 3 times in any category!"
      }
      set :components, [
        {:command => "count", :comparator => {"$gt" => 3}, :where => {:verb => 'comment', :player_id => "%player_id"}}
      ]
      set :adjustment, {
        :points => 3
      }
      set :active, true
      set :allow_duplicates, false
    end

    r_db_commenter = reward 'database commenter' do
      set :reward_template, {
        :message => "You have commented more than 3 times in the database category!"
      }
      set :components, [
        {:command => "count", :comparator => {"$gt" => 3}, :where => {:verb => 'comment', :player_id => "%player_id", :category_slug => "database"}}
      ]
      set :adjustment, {
        :points => 3
      }
      set :active, true
      set :allow_duplicates, false
    end

    mission 'community member' do
      # allow a player to complete this mission more than once?
      set :repeatable, false
      set :active, true
      # give points and reward upon completing mission? (This is required for track progression)
      set :enable_rewards, true
      # points and secondary units to award when completing a mission
      set :adjustment, {
        :points => 10
      }
      set :reward_definition_ids do [r_active_commenter._id, r_db_commenter._id] end
    end
    # reward 'top commenter' do
    #   set :reward_template, {
    #     :message => "Congrats!"
    #   }
    #   set :components, [
    #     {:command => "count", :comparator => 1, :where => {:verb => 'comment', :player_id => "%player_id"}},
    #     # the first player who performs at least twice the 'new_question' behaviour
    #     {:command => "count", :comparator => {"$gt" => 2}, :where => {:verb => 'new_question', :site_id => "%site_id"}}
    #   ]
    #   set :conjunction, "and"
    #   set :adjustment, {
    #     :points => 5
    #   }
    #   set :active, true
    #   set :allow_duplicates, false
    # end
  end
end

network.save!
